<template>
	<loading-wait v-if="loading" ></loading-wait>
	<view class="uni-global-box" >
		<view class="uni-global-title">
			权限管理 / 后台管理员
		</view>
		<view class="body">
			<view class="class-manage-top">
				<view class="">
					<u-button type="primary" size="mini" @click="goUrl('./add')" >
						<text class="iconfont icon-shuliang-zengjia"></text>
						<text> &nbsp;新增管理员</text>
					</u-button>
				</view>
			</view>
			<u-table class="u-table-senior" >
				<u-tr style="margin-right:15px;" >
					<u-th class="u-table-th"> # </u-th>
					<u-th class="u-table-th">用户昵称</u-th>
					<u-th class="u-table-th">用户名</u-th>
					<u-th class="u-table-th">手机号</u-th>
					<u-th class="u-table-th">状态</u-th>
					<u-th class="u-table-th">注册时间</u-th>
					<u-th  width="150px" >操作</u-th>
				</u-tr>
				<view class="u-table-senior-body" style="height:65vh" >
					<loaders  v-if="data.length == 0" ></loaders>
					<u-tr  v-for="(item,index) in data" :key="index"
						class="u-table-tr"
					>
						<u-td class="u-table-td" >{{index<9?'0':''}}{{index +1 }}</u-td>
						<u-td class="u-table-td">{{item.nickname}}</u-td>
						<u-td class="u-table-td">{{item.username}}</u-td>
						<u-td class="u-table-td">{{item.mobile || '暂未设置' }}</u-td>
						<u-td class="u-table-td">
							{{item.status ? '✅正常' :'❌禁用'}}
						</u-td>
						<u-td class="u-table-td">
							<uni-dateformat :date="item.register_date" ></uni-dateformat>
						</u-td>
						<u-td class="u-table-td " width="150px">
							<view class="u-table-td-util">
								<u-button size="mini" type="primary" @click="update(item._id)"  :disabled="item.username == 'admin'" >
									<text class="iconfont icon-xiugai"></text>
									<text>修改</text>
								</u-button>
								<u-button size="mini" type="error" @click="removeData(item,index)" :disabled="item.username == 'admin'" >
									<text class="iconfont icon-lajitong"></text>
									<text>删除</text>
								</u-button>
							</view>
							
						</u-td>
					</u-tr>
				</view>
				<view class="u-table-senior-pagination">
					<uni-pagination @change="enventPages" :total="count"></uni-pagination>
				</view>
			</u-table>
		</view>
	</view>
	<u-toast ref="uToast" />
</template>

<script>
	const global = uniCloud.importObject("global",{customUI:true});
	export default {
		data() {
			return {
				data:[],
				// 分页
				limit:10,
				skip:0,
				count:0,
			};
		},
		onLoad() {
			this.getData() ;
		},
		methods:{
			getData(){
				uniCloud.importObject("admin",{customUI:true}).getAdmin().then(result => {
					const data = result.result.data ;
					this.data = data;
					this.count = result.result.count ;
				})
			},
			// 删除数据
			removeData(item,index){
				const USER_ID = uni.getStorageSync('uni-id-pages-userInfo')._id;
				if(USER_ID != '66d91a893f1a47637377985e') return this.$refs.uToast.show({title: '您无权删除管理员信息',type: 'error',duration:2000,position:'top',}); 

				uni.showModal({title:"您确认删除吗?",content:"数据删除后不可回复,请慎重"}).then(res =>  {
					if(!res.confirm) return ;
					uniCloud.importObject("admin",{customUI:true}).deleteAdmin(item._id).then(result => {
						if(result.code == 200){
							this.$refs.uToast.show({
								title:result.message,type: 'success',duration:1500,position:'top',
								callback:()=>{
									this.data.splice(index,1)
								}
							})
						}else {
							this.$refs.uToast.show({title:result.message,type: 'error',duration:2000,position:'top' })
						}
					})
				})
			},
			// 修改管理员
			update(uid){
				const USER_ID = uni.getStorageSync('uni-id-pages-userInfo')._id;
				if(uid == '66d91a893f1a47637377985e'){
					if(USER_ID == '66d91a893f1a47637377985e'){
						uni.navigateTo({url:'./update?uid='+uid});
					}else{
						return this.$refs.uToast.show({title: '您无权修改超级管理员信息',type: 'error',duration:2000,position:'top',})
					}
				}
				uni.navigateTo({url:'./update?uid='+uid})
			},
			// 分页
			enventPages(val){
				this.loading = true
				this.skip = ( val.current-1) * 10 ;
				this.getData()
			},
			goUrl(url){
				uni.navigateTo({url})
			},
		}
	}
</script>

<style lang="scss">
	.class-manage-top{
		display: flex;
		padding:5px 20px ;
		justify-content:flex-end;
	}
</style>
